<?php
final class system_passport {
	
	public static $_namespace = 'PASSPORT';
	public static $_name = 'auth';
	private static $_instance = null;
	/**
	 * @return system_passport
	 */
	public static function getInstance() {
		if(null === self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	private $_storage = null;
	/**
	 * @return system_passport
	 */
	public function setStorage($storage) {
		if($storage instanceof system_passport_storage_interface) {
			$this->_storage = $storage;
		} else {
			$class = 'system_passport_storage_' . $storage;
			$this->_storage = new $class(self::$_namespace);
		}
		return $this;
	}
	/**
	 * 自动判断存储媒介
	 * @return system_passport_storage_abstract
	 */
	public function getStorage() {
		if($this->_storage === null) {
			$namespace = system_session::namespaceGet(self::$_namespace);
			if($namespace && isset($namespace[self::$_name])) {
				$this->_storage = new system_passport_storage_session(self::$_namespace);
			} else {
				$this->_storage = new system_passport_storage_cookie(self::$_namespace);
			}
		}
		return $this->_storage;
	}
}
